<?php
/********************************************************
GetExpressCheckoutDetails.php

This functionality is called after the buyer returns from
PayPal and has authorized the payment.

Displays the payer details returned by the
GetExpressCheckoutDetails response and calls
DoExpressCheckoutPayment.php to complete the payment
authorization.

Called by ReviewOrder.php.

Calls DoExpressCheckoutPayment.php and APIError.php.

********************************************************/
if(!isset($_SESSION))
{
    session_start();  
}


/* Collect the necessary information to complete the
   authorization for the PayPal payment
   */

$_SESSION['token']=$_REQUEST['token'];
$_SESSION['payer_id'] = $_REQUEST['PayerID'];


$_SESSION['currCodeType']=$_REQUEST['currencyCodeType'];
$_SESSION['paymentType']=$_REQUEST['paymentType'];



/* Display the  API response back to the browser .
   If the response from PayPal was a success, display the response parameters
   */

   
$cart = unserialize($_SESSION['cart']);

$shipping_first_name = $_SESSION['shipping_first_name'];
$shipping_last_name = $_SESSION['shipping_last_name'];
$shipping_full_name = $shipping_first_name . " " . $shipping_last_name;
$shipping_street_address = $_SESSION['shipping_street_address'];
$shipping_city = $_SESSION['shipping_city'];
$shipping_state = $_SESSION['shipping_state'];
$shipping_zip_code = $_SESSION['shipping_zip_code'];
$shipping_carrier_name = $_SESSION['carrier_name'];
$service_name = $_SESSION['service_name'];
$service_id = $_SESSION['service_id'];
$carrier_name = $_SESSION['carrier_name'];
$items_total = number_format($_SESSION['items_total'], 2, '.', '');      
$shipping_total = number_format($_SESSION['shipping_total'], 2, '.', '');
$total = number_format($_SESSION['total_after_shipping'], 2, '.', '');; 
$shiptocountrycode =    "US";   
   
?>


    
    
    <br /><br />
<?php
   
        
   

   $itemarray = array();
    $categoryarray = array();
    $quantityarray = array();
    $priceperitemarray = array();
    $subtotalarray = array();
    $cartnumber = 0;
    foreach ($cart->items as $id => $info) 
    {   
        $cart_sw_id = $info['sw_id'];        
        $cart_info = "<tr align='center'>";
        $category = $info['category'];
        $name = $info['name']; 
        $name = urldecode($name);
        $qty = $info['qty'];
        $price = number_format($info['price'], 2, '.', '');
        $subtotal = $qty * $price;
        $subtotal = number_format($subtotal, 2, '.', '');      
        if($cart_sw_id == "")
        {            
            $itemarray[$cartnumber] = $name;           
            $categoryarray[$cartnumber] = $category;            
            $quantityarray[$cartnumber] = $qty;            
            $priceperitemarray[$cartnumber] = "$$price";           
            $subtotalarray[$cartnumber] = "$$subtotal";
           
        }
        else
        {
            $xml_name = new SimpleXMLElement($name);
            $gen_name = $xml_name->gw_name;
            
            $xml_build = "";
            foreach($xml_name->Item as $eachitem1)
            {
                $desc_category = $eachitem1->category;
                $descriptor = $eachitem1->descriptor;    
                $xml_build .= $desc_category . ":" . $descriptor . " ";
            }
            $name = $gen_name . "<br />" . $xml_build;
            
            $itemarray[$cartnumber] = $name;                       
            $categoryarray[$cartnumber] = $category;                 
            $quantityarray[$cartnumber] = $qty;              
            $priceperitemarray[$cartnumber] = "$$price";          
            $subtotalarray[$cartnumber] = "$$subtotal";   
           
        }
        
        $cartnumber ++;            
    } 
echo '<h2><b>Items Being Purchased</b></h2>';    
echo '<div class="table">';
echo '<ul>'; 
echo '<li class="title">Item</li>';
$num = 0;
while($num != $cartnumber)
{
    if($num % 2 == 0)
    {
        echo "<li class='even'>$itemarray[$num]</li>";   
    }
    else
    {
        echo "<li class='odd'>$itemarray[$num]</li>";   
    }
    
    $num++;
}
echo '</ul>';

echo '<ul>'; 
echo '<li class="title">Category</li>';
$num = 0;
while($num != $cartnumber)
{
    if($num % 2 == 0)
    {
        echo "<li class='even'>$categoryarray[$num]</li>";   
    }
    else
    {
        echo "<li class='odd'>$categoryarray[$num]</li>";   
    }
    
    $num++;
}
echo '</ul>';  
echo '<ul>'; 
echo '<li class="title">Quantity</li>';
$num = 0;
while($num != $cartnumber)
{
    if($num % 2 == 0)
    {
        echo "<li class='even'>$quantityarray[$num]</li>";   
    }
    else
    {
        echo "<li class='odd'>$quantityarray[$num]</li>";   
    }
    
    $num++;
}
echo '</ul>';  
echo '<ul>'; 
echo '<li class="title">Price Per Item</li>';
$num = 0;
while($num != $cartnumber)
{
    if($num % 2 == 0)
    {
        echo "<li class='even'>$priceperitemarray[$num]</li>";   
    }
    else
    {
        echo "<li class='odd'>$priceperitemarray[$num]</li>";   
    }
    
    $num++;
}
echo '</ul>';  
echo '<ul>'; 
echo '<li class="title">Subtotal</li>';
$num = 0;
while($num != $cartnumber)
{
    if($num % 2 == 0)
    {
        echo "<li class='even'>$subtotalarray[$num]</li>";   
    }
    else
    {
        echo "<li class='odd'>$subtotalarray[$num]</li>";   
    }
    
    $num++;
}
?>
</ul>  
</div>
<br /><br /><br /><br /><br /><br />
<div class="table">
    <ul>
    <li class="title">
    <b>Total For Items</b>
    </li>     
    <li class="even">     
    $<?php print $items_total;?>
    </li>
    </ul>
    </div>
    <br /><br />    
    <br /><br /> 
     <a href="cart.php">Edit</a>  
     <br />  
    
<?php  

 print "
    <h2><b>Shipping Information</b></h2>
      <div class='table'>
      <ul>
            <li class='title'>
                <b>Recipient's Name</b>
            </li>
            <li class='even'>
                $shipping_first_name  $shipping_last_name 
            </li>
      </ul>
      <ul>
            <li class='title'>
                <b>Destination Address</b>
            </li>
            <li class='even'>
                $shipping_street_address
            </li>
      </ul>
      <ul>
            <li class='title'>
                <b>City</b>
            </li>
            <li class='even'>
                $shipping_city
            </li>
      </ul>
      <ul>
            <li class='title'>
                <b>State</b>
            </li>
            <li class='even'>
                $shipping_state
            </li>
      </ul>
      <ul>
            <li class='title'>
                <b>Zipcode</b>
            </li>
            <li class='even'>
                $shipping_zip_code
            </li>
      </ul>
      <ul>
            <li class='title'>
                <b>Shipping Service</b>
            </li>
             <li class='even'>
                $shipping_carrier_name:$service_name
            </li>
      </ul>
       <ul>
            <li class='title'>
                <b>Shipping Total</b>
            </li>
             <li class='even'>
                $ $shipping_total
            </li>
      </ul>
      </div>
      ";
  echo '<br /><br /><br /><br />';  
   echo ' <a href="orderinformation.php">Edit</a>';

     $total_after_shipping = number_format($_SESSION['total_after_shipping'], 2, '.', '');       
  ?>
   <h2><b>Total After Shipping</b></h2>        
    <div class="table">
        <ul>
            <li class="title">
                <b>Sum Total</b></th>
            </li>
            <li class="even">
                <b><?php echo "$" . $total ?></b>
            </li>
        </ul>
    </div>  
    
    <br /><br /><br />
	<form action="DoExpressCheckoutPayment.php" id="finalizedform" method="POST" onsubmit="submitfinal.disabled=true; return true;"> 
	 <center>
           <table width =70>
            
            <tr>
                <td><h2><b>Order Total:</b></h2></td>
                <td>
                  <?php  echo "<h2>$" .  $total_after_shipping . "</h2>"; ?>
                </td>
            </tr>
            

          
            <tr>
                <td class="thinfield">
                     <input type="submit" id="submitfinal" value="Pay" />
                </td>
            </tr>
        </table>
    </center>
    </form>

</body>
</html>
